import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:test/constants/environment.dart';
import 'package:test/network/interceptors/token_interceptor.dart';

class DioHelper {
  static Dio? _dio;
  static WidgetRef? _ref;

  static void init(WidgetRef ref) {
    _ref = ref;
  }

  static Dio getInstance() {
    if (_dio == null) {
      // 创建 Dio 实例
      _dio = Dio(BaseOptions(
        baseUrl: isReleaseMode ? 'https://www.baidu.com' : 'http://127.0.0.1:8080/',
        connectTimeout: const Duration(seconds: 5),
        receiveTimeout: const Duration(seconds: 5),
      ));

      // 添加拦截器
      _dio!.interceptors.add(LogInterceptor(responseBody: true));
      _dio!.interceptors.add(TokenInterceptor(_ref!));
    }
    return _dio!;
  }
}
